package com.blt.car.me;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseFragment;
import com.blt.car.base.ViewHolder;
import com.blt.car.widget.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.other.OutApi;
import blt.app_core.api.other.bean.FeedBackVo;

/**
 * Created by xgp on 2017/6/13.
 */

public class CommitFBFragment extends BaseFragment implements View.OnClickListener {
    private NoScrollGridView gridView;

    //    String vouchers[] = {"改进意见", "改进意见", "改进意见", "改进意见", "改进意见", "改进意见"};
    List<String> dataSource = new ArrayList<>();
    FBCommitAdapter adapter;
    private String feedType;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fb_commit;
    }


    @Override
    protected void initData() {
        super.initData();
        CoreManager.getCore(OutApi.class).getOpinionType();

    }

    @Override
    protected void initView(ViewHolder holder) {
        gridView = holder.get(R.id.fb_gridview);
        adapter = new FBCommitAdapter();
        gridView.setAdapter(adapter);
        //Data
//        List<String> dataSource = new ArrayList<>();

        final EditText area = holder.get(R.id.opinion_et_area);
        holder.get(R.id.btn_commit_fb);
        holder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (feedType == null) {
                    toast("请选择问题类型");
                    return;
                }
                String content = area.getText().toString();
                CoreManager.getCore(OutApi.class).saveOpinion(feedType, content);
                toast("提交成功,等待处理");
            }
        }, R.id.btn_commit_fb);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    List<FeedBackVo> feedBackVos = new ArrayList<>();

    @CoreEvent(coreClientClass = OutApi.class)
    public void onOpinionType(List<FeedBackVo> list) {
        feedBackVos.clear();
        feedBackVos.addAll(list);
        for (FeedBackVo vo : list) {
            dataSource.add(vo.dataValue);
        }
        adapter.set();

    }

    class FBCommitAdapter extends BaseAdapter {
        private SparseArray<Boolean> selects = new SparseArray<>();

        //        public FBCommitAdapter() {
//            for (int i = 0; i < vouchers.length; i++) {
//
//            }
//        }
        public void set() {
            for (int i = 0; i < dataSource.size(); i++) {
                selects.put(i, false);
            }
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return dataSource.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            if (v == null) {
                v = LayoutInflater.from(getContext()).inflate(R.layout.item_fbcommit, parent, false);
            }

            final LinearLayout container = (LinearLayout) v.findViewById(R.id.item_fbq_container);
            final TextView fbq = (TextView) v.findViewById(R.id.item_fbq);

            fbq.setText(dataSource.get(position));

            if (selects.get(position)) {
                container.setBackgroundResource(R.drawable.bg_shape_selectfbq);
                fbq.setTextColor(getResources().getColor(R.color.text_color));
            } else {
                container.setBackgroundResource(R.drawable.bg_shape_noselectfbq);
                fbq.setTextColor(getResources().getColor(R.color.text_color_black));
            }

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < dataSource.size(); i++) {
                        selects.put(i, false);
                        if (i == position) {
                            feedType = feedBackVos.get(position).dataKey;
                            selects.put(i, true);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
            return v;
        }


    }

}
